package main.com.dao;


import main.com.entry.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author lk
 * @date 2020/6/23 11:16
 * @description: 用户相关数据处理的CRUD
 */

public class UserDao extends BaseDao {

    private PreparedStatement ps = null;
    private ResultSet rs = null;
    private Connection con = null;

    /**
     * 保存用户
     *
     * @param user
     */
    public boolean saveUser(User user) {
        con = this.getConnection();
        try {
            String sql = "insert into user(username,password,email,workplace,addtime) values(?,?,?,?,?)";
            ps = con.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getEmail());
            ps.setString(4, user.getWorkplace());
            ps.setString(5, user.getAddtime());
            ps.execute();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            this.closeConnection(con);
        }
    }

    /**
     * 根据用户名查询用户密码
     *
     * @param username
     * @return
     */
    public String checkUser(String username) {
        con = this.getConnection();
        String password = null;
        try {
            String sql = "select password from user where username =? ";
            ps = con.prepareStatement(sql);
            ps.setString(1, username);
            rs = ps.executeQuery();
            if (rs.next()) {
                password = rs.getString(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.closeConnection(con);
            return password;
        }
    }

    /**
     * 根据用户email查询用户名
     *
     * @param email
     * @return
     */
    public String checkEmail(String email) {
        con = this.getConnection();
        String username = null;
        try {
            String sql = "select username from user where email =? ";
            ps = con.prepareStatement(sql);
            ps.setString(1, email);
            rs = ps.executeQuery();
            if (rs.next()) {
                username = rs.getString(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.closeConnection(con);
            return username;
        }
    }

    /**
     * 根据用户名查询用户所有信息
     *
     * @param username
     * @return
     */
    public User selectAll(String username) {
        con = this.getConnection();
        User user = new User();
        try {
            String sql = "select * from user where username =? ";
            ps = con.prepareStatement(sql);
            ps.setString(1, username);
            rs = ps.executeQuery();
            if (rs.next()) {
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setWorkplace(rs.getString("workplace"));
                user.setAddtime(rs.getString("addtime"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.closeConnection(con);
            return user;
        }
    }

    /**
     * 更新邮箱地址
     *
     * @param email
     * @param username
     */
    public boolean updateEmail(String email, String username) {
        con = this.getConnection();
        try {
            String sql = "update user set email = ? where username = ?";
            ps = con.prepareStatement(sql);
            ps.setString(1, email);
            ps.setString(2, username);
            ps.execute();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            this.closeConnection(con);
        }
    }

    /**
     * 更新密码
     *
     * @param password
     * @param username
     */
    public boolean updatePassword(String password, String username) {
        con = this.getConnection();
        try {
            String sql = "update user set password = ? where username = ?";
            ps = con.prepareStatement(sql);
            ps.setString(1, password);
            ps.setString(2, username);
            ps.execute();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            this.closeConnection(con);
        }
    }

}
